package com.cc.Controller;
import com.cc.Services.CommentServicesImpl;
import com.cc.entity.Comment;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/Comments")
public class CommentController {
    @Autowired
    CommentServicesImpl commentServicesImpl;
    @RequestMapping("/findComment")
    public String find(@RequestParam(value = "page",defaultValue = "1")int page,
                       @RequestParam(value = "pageSize",defaultValue = "3") int pageSize,Model model){
        List<Comment> comments=commentServicesImpl.listByPage(page,pageSize);
        double t=(double)commentServicesImpl.total()/pageSize;
        int total=(int)Math.ceil(t);
        model.addAttribute("comments",comments);
        model.addAttribute("curPage",page);
        model.addAttribute("total",total);
        return "manage/comments";
    }
    @RequestMapping("/cdelete")
    public String delete(Integer id){
        int result=commentServicesImpl.deleteByid(id);
        if(result==0){

        }
        return "manage/manage-result";
    }
    @RequestMapping("/cedit")
    public String cedit(Model model,int id){
        System.out.println(id);
     Comment comment=commentServicesImpl.selectByCondition(id);
     model.addAttribute(comment);
        return "manage/comment-modify";
    }
    @RequestMapping("/ceditSubmit")
    public String ceditSubmit(Comment comment){
        System.out.println(comment);
        int result=commentServicesImpl.updateById(comment);
        System.out.println(result);
        if(result<=0){
            return "manage/error";
        }
        return "manage/manage-result";
    }

}

